namespace UmSoft.SharePoint.Integration._1Cv77
{
    using System;
    using UmSoft.SharePoint.Integration._1Cv77;

    public class V7ReferenceElement : V7Object
    {
        private V7Reference reference;
        internal V7ReferenceElement(V7Object v7Object, V7Reference reference) : base(v7Object)
        {
            this.reference = reference;
        }
        public void Save()
        {
            this.Reference.InvokeMethod("Записать", new object[0]);   // Evaluation version. // Evaluation version. // Evaluation version.
        }
        public void Delete(bool isMark)
        {
            Reference.InvokeMethod("Удалить",
                new object[] {
                        (isMark ? V7.TRUE : V7.FALSE)});
        }
        public void Delete()
        {
            this.Delete(true);

        }
        public override void SetProperty(string propertyName, object value)
        {
            if (Reference == null)
            {
                base.SetProperty(propertyName, value);
                return;
            }
            Reference.SetProperty(propertyName, value);
        }
        public override object GetProperty(string propertyName)
        {
            object property = null;
            if (this.Reference != null)
            {
                property = this.Reference.GetProperty(propertyName);
            }
            else
            {
                property = base.GetProperty(propertyName);
            }
            if (property is V7Object)
            {
                switch ((property as V7Object).Type)
                {
                    case AggregateType.Enum:
                        return new V7EnumElement(property as V7Object);

                    case AggregateType.Reference:
                        return new V7ReferenceElement(property as V7Object, null);

                    case AggregateType.Periodic:
                        return new V7Periodic(property as V7Object);
                }
            }
            return property;

        }
        public override object InvokeMethod(string methodName, params object[] args)
        {
            if (Reference == null)
            {
                return base.InvokeMethod(methodName, args);
            }
            else
            {
                return Reference.InvokeMethod(methodName, args);
            }
        }
        protected V7Reference Reference
        {
            get
            {
                return this.reference;

            }
        }
        public bool IsGroup
        {
            get
            {
                return base.InvokeMethod("ЭтоГруппа",
                    new object[0]).Equals(V7.TRUE);
            }
        }
        public V7ReferenceElement Parent
        {
            get
            {
                return new V7ReferenceElement(this.GetProperty("Родитель") as V7Object, null);

            }
            set
            {
                base.SetProperty("Родитель", value);
            }
        }
        public V7ReferenceElement Owner
        {
            get
            {
                return new V7ReferenceElement(this.GetProperty("Владелец") as V7Object, null);

            }
            set
            {
                base.SetProperty("Владелец", value);
            }
        }
        public string Code
        {
            get
            {
                return this.GetProperty("Код").ToString();

            }
            set
            {
                base.SetProperty("Код", value);
            }
        }
        public string Name
        {
            get
            {
                return this.GetProperty("Наименование").ToString();

            }
            set
            {
                base.SetProperty("Наименование", value);
            }
        }
        public int Level
        {
            get
            {
                object obj2 = this.InvokeMethod("Уровень", new object[0]);
                if (obj2 == null)
                {
                    throw new Exception("Ошибка в запросе уровня элемента");
                }
                return Convert.ToInt32(obj2);
            }
        }
    }
}
